<?php

namespace App\Http\Controllers;

use App\Http\Lib\Output;
use App\Http\Lib\HttpClient;

trait ControllerExtension
{
    # 错误页面
    public function errorPage($errcode = 10000, $errmessage = 'Error')
    {
        return Output::errorPage($errcode, $errmessage);
    }

    # 成功页面
    public function successPage($message = 'OK')
    {
        return Output::successPage($message);
    }

    # 错误接口
//     public function e($errcode = 10000, $errmessage = 'Error', $assist = false)
//     {
//         return Output::e($errcode, $errmessage, $assist);
//     }

//     # 成功接口
//     public function o($data = [], $code = 0, $trigger = 'none')
//     {
//         return Output::o($data, $code, $trigger);
//     }

    # 组装token
    public function makeToken(array $data = [])
    {
        $data['time'] = time();
        $data['app'] = env('PRIVATE_APP_NAME');
        $data['token'] = md5($data['app'].$data['time'].env('PRIVATE_APP_TICKET'));
        return $data;
    }

    # 代理请求接口
    public function privateApi($method, $url, array $data = [],  $with_token = false)
    {
//         $data['uid'] = \Session::get('uid');

//         if ( $data['uid'] == null )
//             return $this->e('登录状态失效');

        if ( $with_token )
            $data = $this->makeToken($data);

        $r = HttpClient::request($method, $url, $data);
        if ( $r )
            return $r;
        return false;
    }

}
